/**
 * This class will implement the rate setting controller to our system. 
 */


package control;

// Imports ********************************************

import java.io.Serializable;
import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import system.VcpClient;
import vcpClasses.ParkingLot;
import vcpClasses.User;

// Start of SetRatesController ***************************

public class ComplaintController extends AbstractController {

	private SystemTypes sysType;
	private boolean returned = false;
	private boolean reqStatus;
	
	//Constructors *******************************************
	
	/**
	 * Unimplemented constructor
	 */
	
	public ComplaintController(){};
	/**
	 * This method will receive all data from the GUI,
	 * will try to initiate connection to the server,
	 * if succeed, will send the request to the server and will return answer accordingly. 
	 * @param ip - user given IP for the server
	 * @param port - user given port for the server
	 * @param obj - this will be an instance of User that will be initialized at the GUI, according to user input
	 * @return - false if connection to server failed, or if verifying went wrong, and true otherwise.
	 */
	
	// Controller methods ****************************************
	
	/**
	 * this method will send new complaint to all customer service workers
	 * @param sysType - the complaint to issue
	 * @return - the proper result message
	 */
	
	public SystemTypes SendComplaint(SystemTypes sysType)
	{
		returned = false;
		sysType.setSysMode(SystemMode.NEW_COMPLAINT);
		boolean flag = SendToServer(sysType);
		while(!returned){}
		if (!flag)
		{
			switch (this.sysType.getSysMode())
			{
				case SYS_ERR:
				{
					this.sysType.setObj("Client - Server Connection Failed!");
					break;
				}
				case FAIL:
				{
					this.sysType.setObj("Can't send confirmation request !");
					break;
				}
				case DB_CON_ERR:
				{
					this.sysType.setObj("DB connection Failed!");
					break;
				}
				case EMPTY_RES:
				{
					this.sysType.setObj("No registered Network Manager !");
					break;
				}
				
			default:
				this.sysType.setObj("Unknown failure in process!");
				break;
			}
		}
		else
			this.sysType.setObj("Complaint sent to Customer Service.");
		
		return this.sysType;
	}
	
	/**
	 * this method will the send the reply for the complaint
	 * @param sysType - the complaint to reply
	 * @return - the proper result
	 */
	
	public SystemTypes SendComplaintReply(SystemTypes sysType)
	{
		returned = false;
		sysType.setSysMode(SystemMode.COMPLAINT_REPLY);
		boolean flag = SendToServer(sysType);
		while(!returned){}
		if (!flag)
		{
			switch (this.sysType.getSysMode())
			{
				case SYS_ERR:
				{
					this.sysType.setObj("Client - Server Connection Failed!");
					break;
				}
				case FAIL:
				{
					this.sysType.setObj("Can't send confirmation request !");
					break;
				}
				case DB_CON_ERR:
				{
					this.sysType.setObj("DB connection Failed!");
					break;
				}
				case EMPTY_RES:
				{
					this.sysType.setObj("No registered Network Manager !");
					break;
				}
				
			default:
				this.sysType.setObj("Unknown failure in process!");
				break;
			}
		}
		else
			this.sysType.setObj("Reply was sent succsessfully !");
		
		return this.sysType;
	}
	
	/**
	 * this method will update refund to the clients balance
	 * @param sysType - the client to refund
	 * @return - the proper result
	 */
	
	public SystemTypes SendRefund(SystemTypes sysType)
	{
		returned = false;
		sysType.setSysMode(SystemMode.REFUND);
		boolean flag = SendToServer(sysType);
		while(!returned){}
		if (!flag)
		{
			switch (this.sysType.getSysMode())
			{
				case SYS_ERR:
				{
					this.sysType.setObj("Client - Server Connection Failed!");
					break;
				}
				case FAIL:
				{
					this.sysType.setObj("Can't send confirmation request !");
					break;
				}
				case DB_CON_ERR:
				{
					this.sysType.setObj("DB connection Failed!");
					break;
				}
				case EMPTY_RES:
				{
					this.sysType.setObj("No registered Network Manager !");
					break;
				}
				
			default:
				this.sysType.setObj("Unknown failure in process!");
				break;
			}
		}
		else
			this.sysType.setObj("Refund was sent to the client.");
		
		return this.sysType;
	}

		
	@Override
	public boolean SendToServer(Serializable obj) {
		VCP.client.send(obj);
		sysType = (SystemTypes) VCP.client.getResult();
		if (sysType.getSysMode() != SystemMode.OK)
		{
			returned = true;
			return false;
		}
		returned = true;
		return true;
	}
	
	// Getters and Setters ***************************************************

		/**
		 * @return the sysType
		 */
		public SystemTypes getSysType() {
			return sysType;
		}

		/**
		 * @param sysType the sysType to set
		 */
		public void setSysType(SystemTypes sysType) {
			this.sysType = sysType;
		}

		/**
		 * @return the returned
		 */
		public boolean isReturned() {
			return returned;
		}

		/**
		 * @param returned the returned to set
		 */
		public void setReturned(boolean returned) {
			this.returned = returned;
		}
	

}
